package com.evzgaga.evzrecon.xmlrpc.client;

import java.io.OutputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import org.apache.ws.commons.serialize.XMLWriter;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.common.XmlRpcStreamConfig;
import org.apache.xmlrpc.serializer.BaseXmlWriterFactory;
import org.xml.sax.ContentHandler;

/**
 *
 * @author Bastien
 */
public class GbxXmlWriterFactory extends BaseXmlWriterFactory {

    @Override
    protected XMLWriter newXmlWriter() {
        return new GbxXmlWriter();
    }

    @Override
    public ContentHandler getXmlWriter(XmlRpcStreamConfig pConfig, OutputStream pStream) throws XmlRpcException {
        GbxXmlWriter xw = (GbxXmlWriter) newXmlWriter();

        xw.setDeclarating(true);
        String enc = pConfig.getEncoding();
        if (enc == null) {
            enc = XmlRpcStreamConfig.UTF8_ENCODING;
        }
        xw.setEncoding(enc);
        xw.setIndenting(false);
        xw.setFlushing(true);
        
        xw.setWriter(new StringWriter());

        return xw;
    }
}
